package com.tbvtc.agriculturalcreditchainserver2025.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tbvtc.agriculturalcreditchainserver2025.dto.Cart.*;
import com.tbvtc.agriculturalcreditchainserver2025.dto.consumer.addCartDto;
import com.tbvtc.agriculturalcreditchainserver2025.dto.order.OrdersPaymentDTO;
import com.tbvtc.agriculturalcreditchainserver2025.entity.Crop;
import com.tbvtc.agriculturalcreditchainserver2025.entity.User;
import com.tbvtc.agriculturalcreditchainserver2025.pojo.ResultPage;
import com.tbvtc.agriculturalcreditchainserver2025.service.CartService;
import com.tbvtc.agriculturalcreditchainserver2025.service.ICropService;
import com.tbvtc.agriculturalcreditchainserver2025.service.IUserService;
import com.tbvtc.agriculturalcreditchainserver2025.utils.RUtils;
import com.tbvtc.agriculturalcreditchainserver2025.utils.Result;
import com.tbvtc.agriculturalcreditchainserver2025.utils.voice.VoiceFileUtil;
import com.tbvtc.agriculturalcreditchainserver2025.vo.Cart.*;
import com.tbvtc.agriculturalcreditchainserver2025.vo.ConsumerVo.addCartVo;
import com.tbvtc.agriculturalcreditchainserver2025.vo.order.OrderPaymentVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Slf4j
@CrossOrigin
@Tag(name = "购物车相关接口")
@RestController
@RequestMapping("/cart")
public class cartController {

    @Autowired
    private CartService cartService;
    @Resource
    private IUserService userService;
    @Resource
    private ICropService cropService;



    /**
     * 加入购物车
     *
     * @param addCartDto
     * @return
     */
    @Operation(summary = "加入购物车")
    @PostMapping("/addCart")
    public Result addCart(@RequestBody addCartDto addCartDto) {
        User user = userService.getById(addCartDto.getUserId());
        if (user == null) {
            return RUtils.Err(0, "用户不存在");
        }
        Crop crop = cropService.getById(addCartDto.getGoodId());
        if (crop == null) {
            return RUtils.Err(0, "商品不存在");
        }
        addCartVo addCartVo = cartService.addCart(addCartDto);
        return RUtils.success(addCartVo);
    }

    @GetMapping("/getCartInfo")
    @Operation(summary = "根据userId获取购物车信息")
    public Result getCartInfo(Integer userId) {
        return RUtils.success(cartService.getCartInfo(userId));
    }

    @Operation(summary = "修改购物车数量")
    @PostMapping("/updateCartNum")
    @Parameters({
            @Parameter(name = "id", description = "购物车id"),
            @Parameter(name = "count", description = "数量")
    })
    public Result updateCartNum(Integer id, Integer count) {
        Integer count1 = cartService.updateCartNum(count, id);
        return RUtils.success(count1);
    }

    @Operation(summary = "添加下单地址")
    @PostMapping("/selectAddress")

    public Result addAddress(@RequestBody cartAddressDto cartAddressDto) {
        cartService.addAddress(cartAddressDto);
        return RUtils.success();
    }

    @Operation(summary = "清空购物车")
    @PostMapping("/clearCart")
    public Result clearCart(Integer userId) {
        cartService.clearCart(userId);
        return RUtils.success();
    }

    @Operation(summary = "结算购物车")
    @PostMapping("/settlementCart")
    public Result settlementCart(@RequestBody SettlementCartDto settlementCartDto) {
        cartIdAndPriceVo cartIdAndPriceVo = cartService.settlementCart(settlementCartDto);
        return RUtils.success(cartIdAndPriceVo);
    }

    @Operation(summary = "删除购物车单个商品")
    @PostMapping("/deleteCart")
    @Parameter(name = "cartId", description = "购物车id")
    public Result deleteCart(Integer cartId) {
        cartService.removeCart(cartId);
        return RUtils.success();
    }

    @Operation(summary = "支付完毕")
    @PostMapping("/pay")
    public Result pay(@RequestBody SettlementCartDto settlementCartDto) {
        cartService.pay(settlementCartDto);
        return RUtils.success();
    }


    @Operation(summary = "根据用户id查看用户订单详情")
    @PostMapping("/findOrderByUserId")
    @Parameters({
            @Parameter(name = "userId", description = "用户id"),
            @Parameter(name = "state", description = "订单状态0为未支付，1为代发货")
    })
    public Result findOrderByUserId(Integer userId, Integer state) {
        List<CartSelectUserByUserId> list = cartService.UserfindUserOrderById(userId, state);
        return RUtils.success(list);
    }

    @Operation(summary = "支付失败")
    @PostMapping("/cancelPay")
    public Result cancelPay(@RequestBody SettlementCartDto settlementCartDto) {
        cartService.cancelPayCart(settlementCartDto);
        return RUtils.success();
    }

    @Operation(summary = "用户支付未支付的订单")
    @PostMapping("/payOrder")
    @Parameter(name = "orderId", description = "订单id")
    public Result payOrder(String orderId) {
        cartService.payOrder(orderId);
        return RUtils.success();
    }

    @Operation(summary = "查询用户所有地址")
    @PostMapping("/selectAddressByUserId")
    public Result selectAddressByUserId(Integer userId) {
        if (userId == null) {
            return RUtils.Err(0, "用户不存在");
        }
        List<addressItem> addressVos = cartService.selectAddressByUserId(userId);
        return RUtils.success(addressVos);
    }

    @Operation(summary = "根据订单编号查看订单")
    @PostMapping("/findOrderByOrderId")
    public Result findOrderByOrderId(String orderId) {
        if (orderId == null) {
            return RUtils.Err(0, "订单不存在");
        }
        GroupedData list = cartService.findOrderByOrderId(orderId);
        return RUtils.success(list);
    }

    @Operation(summary = "根据地址id删除地址")
    @PostMapping("/deleteAddress")
    @Parameter(name = "addressId", description = "地址id")
    public Result deleteAddress(Integer addressId) {
        if (addressId == null) {
            return RUtils.Err(0, "地址不存在");
        }
        cartService.deleteAddress(addressId);
        return RUtils.success();
    }

    @Operation(summary = "根据地址id修改地址")
    @PostMapping("/updateAddress")

    public Result updateAddress(@RequestBody CartUpdataAddressDto cartUpdataAddressDto) {
        cartService.updateAddress(cartUpdataAddressDto);
        return RUtils.success();
    }


    @Operation(summary = "查看订单剩余时间")
    @PostMapping("/findOrderTime")
    public Result findOrderTime(String orderId) {
        Long orderTime = cartService.findOrderTime(orderId);
        return RUtils.success(orderTime);
    }

    @Operation(summary = "根据地址id修改默认地址")
    @PostMapping("/updateUserAddressSetNonDefault")
    public Result updateUserAddressSetNonDefault(Integer addressId) {
        cartService.updateUserAddressSetNonDefault(addressId);
        return RUtils.success();
    }




}
